package com.ffcs.crmd.tsp.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.ctg.itrdc.platform.common.entity.PageInfo;
import com.ffcs.crmd.tsp.admin.entity.JobDetailDeath;
import com.ffcs.crmd.tsp.admin.service.IJobDetailDeathService;
import com.ffcs.crmd.tsp.api.dto.client.TspJobDetailDeath;
import com.ffcs.crmd.tsp.api.dto.client.TspResult;
import com.ffcs.crmd.tsp.client.core.TspClient;
import com.ffcs.crmd.tsp.common.exception.TspException;
import com.ffcs.crmd.tsp.core.util.BeanUtils;

/**
 * 
 * 功能说明:
 *
 * @author ZHONGFUHUA
 * 
 * @Date 2015年10月20日 下午4:26:46
 *
 *
 * 版本号 | 作者 | 修改时间 | 修改内容
 *
 */
@Service
public class JobDetailDeathServiceImpl extends AbstractService implements IJobDetailDeathService {
    
    @Override
    public void delete(JobDetailDeath jobDetailDeath) throws TspException {
        try {
            TspJobDetailDeath tspJobDetailDeathVO = new TspJobDetailDeath();
            BeanUtils.applyIf(tspJobDetailDeathVO, jobDetailDeath, false);
            
            TspClient tspClient = tspClientService.getClient(jobDetailDeath.getClusterName());
            TspResult<?> tspResult = tspClient.deleteJobDetailDeath(tspJobDetailDeathVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    
    @Override
    public void delete(List<JobDetailDeath> jobDetailDeathList) throws TspException {
        for (JobDetailDeath jobDetailDeath : jobDetailDeathList) {
            delete(jobDetailDeath);
        }
    }
    
    
    @Override
    public List<JobDetailDeath> query(JobDetailDeath jobDetailDeath) throws TspException {
        try {
            TspJobDetailDeath tspJobDetailDeathVO = new TspJobDetailDeath();
            BeanUtils.applyIf(tspJobDetailDeathVO, jobDetailDeath, false);
            
            TspClient tspClient = getClient(jobDetailDeath.getClusterName());
            TspResult<TspJobDetailDeath> tspResult = tspClient.queryJobDetailDeath(tspJobDetailDeathVO);
            validateResult(tspResult);
            
            List<TspJobDetailDeath> tspJobDetailDeathVOList = new ArrayList<TspJobDetailDeath>();
            tspJobDetailDeathVOList.addAll(tspResult.getRetVo().getDataList());
            return BeanUtils.copyList(tspJobDetailDeathVOList, JobDetailDeath.class);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    
    @Override
    public PageInfo<JobDetailDeath> queryPage(JobDetailDeath jobDetailDeath, int pageNumber, int pageSize) throws TspException {
        try {
            TspJobDetailDeath tspJobDetailDeathVO = new TspJobDetailDeath();
            BeanUtils.applyIf(tspJobDetailDeathVO, jobDetailDeath, false);
            tspJobDetailDeathVO.setPageNumber(pageNumber);
            tspJobDetailDeathVO.setPageSize(pageSize);
            
            TspClient tspClient = getClient(jobDetailDeath.getClusterName());
            TspResult<TspJobDetailDeath> tspResult = tspClient.queryJobDetailDeath(tspJobDetailDeathVO);
            validateResult(tspResult);
            
            com.ffcs.crmd.tsp.api.dto.page.PageInfo<TspJobDetailDeath> tspJobDetailDeathPageInfo = tspResult.getRetVo().getPageInfo();
            PageInfo<JobDetailDeath> jobDetailDeathPageInfo = new PageInfo<JobDetailDeath>();
            BeanUtils.applyIf(jobDetailDeathPageInfo, tspResult.getRetVo().getPageInfo());
            jobDetailDeathPageInfo.setList(BeanUtils.copyList(tspJobDetailDeathPageInfo.getList(), JobDetailDeath.class));
            return  jobDetailDeathPageInfo;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public void reset(JobDetailDeath jobDetailDeath) throws TspException {
        try {
            TspJobDetailDeath tspJobDetailDeathVO = new TspJobDetailDeath();
            BeanUtils.applyIf(tspJobDetailDeathVO, jobDetailDeath, false);
            
            TspClient tspClient = tspClientService.getClient(jobDetailDeath.getClusterName());
            TspResult<?> tspResult = tspClient.resetJobDetailDeath(tspJobDetailDeathVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public void reset(List<JobDetailDeath> jobDetailDeathList) throws TspException {
        for (JobDetailDeath jobDetailDeath : jobDetailDeathList) {
            reset(jobDetailDeath);
        }
    }
    
}
